package com.wubai.subject.domain.service.impl;

import com.wubai.subject.common.enums.IsDeletedFlagEnum;
import com.wubai.subject.domain.convert.SubjectLabelConverter;
import com.wubai.subject.domain.entity.SubjectLabelBO;
import com.wubai.subject.domain.service.SubjectCategoryDomainService;
import com.wubai.subject.domain.service.SubjectLabelDomainService;
import com.wubai.subject.infra.basic.entity.SubjectLabel;
import com.wubai.subject.infra.basic.entity.SubjectMapping;
import com.wubai.subject.infra.basic.service.SubjectCategoryService;
import com.wubai.subject.infra.basic.service.SubjectLabelService;
import com.wubai.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);

        return count > 0;
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {

        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList =subjectMappingService.queryDistinctLableId(subjectMapping);
        //判断是否为空
        if(CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }
        List<Long> lableIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> lableList =subjectLabelService.batchQueryById(lableIdList);
        List<SubjectLabelBO>  lableBoList = new ArrayList<>();
        lableList.forEach( lable ->{
            SubjectLabelBO subjectLabelBORes = new SubjectLabelBO();
            subjectLabelBORes.setLabelName(lable.getLabelName());
            subjectLabelBORes.setCategoryId(categoryId);
            subjectLabelBORes.setId(lable.getId());
            lableBoList.add(subjectLabelBORes);

        });
        return lableBoList;
    }
}
